package com.lnj.orders.controller;

import com.lnj.bean.dto.AddressDTO;
import com.lnj.bean.model.Address;
import com.lnj.orders.service.AddressService;
import com.lnj.orders.utils.AddressConvertor;
import com.lnj.utils.ResponseResult;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户收货地址 Controller
 */
@RestController
@RequestMapping("/orders/address")
public class AddressController {

    @Resource
    private AddressService addressService;


    /**
     * 获取指定用户的所有有效地址
     * GET /back/address/list
     *
     * @param userId 从请求头获取用户ID
     * @return 地址列表
     */
    @GetMapping("/list")
    public ResponseEntity<ResponseResult<List<AddressDTO>>> listAddresses(@RequestHeader("X-User-ID") Long userId) {
        List<Address> addresses = addressService.listByUserId(userId);
        List<AddressDTO> dtoList = AddressConvertor.toDtoList(addresses);
        return new ResponseEntity<>(ResponseResult.ok("获取地址列表成功").setData(dtoList), HttpStatus.OK);
    }

    /**
     * 添加新地址
     * POST /back/address
     *
     * @param userId 从请求头获取用户ID
     * @param addressDTO 地址数据
     * @return 添加后的地址信息
     */
    @PostMapping
    public ResponseEntity<ResponseResult<AddressDTO>> addAddress(@RequestHeader("X-User-ID") Long userId,
                                                                 @RequestBody AddressDTO addressDTO) {
        Address address = AddressConvertor.toEntity(addressDTO);
        address.setUserId(userId);
        addressService.addNewAddress(address);
        AddressDTO resultDto = AddressConvertor.toDto(address);
        return new ResponseEntity<>(ResponseResult.ok("添加地址成功").setData( resultDto), HttpStatus.CREATED);
    }

    /**
     * 更新地址信息
     * PUT /back/address/{addressId}
     *
     * @param addressId 地址ID
     * @param userId 从请求头获取用户ID
     * @param addressDTO 地址数据
     * @return 更新后的地址信息
     */
    @PutMapping("/{addressId}")
    public ResponseEntity<ResponseResult<AddressDTO>> updateAddress(@PathVariable("addressId") Long addressId,
                                                                    @RequestHeader("X-User-ID") Long userId,
                                                                    @RequestBody AddressDTO addressDTO) {
        if (!addressId.equals(addressDTO.getAddressId())) {
            return new ResponseEntity<>(ResponseResult.error("路径ID与请求体ID不匹配"), HttpStatus.BAD_REQUEST);
        }
        Address address = AddressConvertor.toEntity(addressDTO);
        address.setUserId(userId); // 确保用户ID正确
        addressService.updateAddressInfo(address);
        AddressDTO resultDto = AddressConvertor.toDto(addressService.getById(addressId)); // 重新查询确保数据一致
        return new ResponseEntity<>(ResponseResult.ok("更新地址成功").setData( resultDto), HttpStatus.OK);
    }

    /**
     * 软删除地址
     * DELETE /back/address/{addressId}
     *
     * @param addressId 地址ID
     * @return 成功信息
     */
    @DeleteMapping("/{addressId}")
    public ResponseEntity<ResponseResult<Void>> deleteAddress(@PathVariable("addressId") Long addressId) {
        addressService.softDeleteAddress(addressId);
        return new ResponseEntity<>(ResponseResult.ok("删除地址成功"), HttpStatus.OK);
    }

    /**
     * 设置某个地址为默认地址
     * PUT /back/orders/address/default/{addressId}
     *
     * @param addressId 地址ID
     * @param userId 从请求头获取用户ID
     * @return 成功信息
     */
    @PutMapping("/default/{addressId}")
    public ResponseEntity<ResponseResult<Void>> setDefaultAddress(@PathVariable("addressId") Long addressId, @RequestHeader("X-User-ID") Long userId) {
        addressService.setDefaultAddress(userId, addressId);
        return new ResponseEntity<>(ResponseResult.ok("设置默认地址成功"), HttpStatus.OK);
    }

    /**
     * 根据地址ID查询地址详情
     * GET /back/orders/address/{addressId}
     *
     * @param addressId 地址ID
     * @return 地址详情
     */
    @GetMapping("/{addressId}")
    public ResponseEntity<ResponseResult<AddressDTO>> getAddressDetail(@PathVariable("addressId") Long addressId) {
        Address address = addressService.getById(addressId);
        AddressDTO addressDTO = AddressConvertor.toDto(address);
        return new ResponseEntity<>(ResponseResult.ok("获取地址详情成功").setData(addressDTO), HttpStatus.OK);
    }

}
